﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;

namespace WebStudio.Framework.Helper
{
    public static class ImageHelper
    {
        // Methods
        public static Image CreateThumbnail(Image original, int percentage)
        {
            try
            {
                if (percentage < 1)
                {
                    throw new Exception("缩放比例不能小于1%");
                }
            }
            catch (Exception objExp)
            {
                ExceptionHelper.Handle(objExp);
            }
            return CreateThumbnail(original, (int)((original.Width * 0.01f) * percentage), (int)((original.Height * 0.01f) * percentage));
        }

        public static Image CreateThumbnail(Image original, int width, int height)
        {
            try
            {
                if (width <= 0)
                {
                    throw new Exception("缩略图宽度必须大于0");
                }
                if (height <= 0)
                {
                    throw new Exception("缩略图高度必须大于0");
                }
            }
            catch (Exception objExp)
            {
                ExceptionHelper.Handle(objExp);
                return null;
            }
            Bitmap bitmap = new Bitmap(width, height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            graphics.DrawImage(original, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
            graphics.Dispose();
            return bitmap;
        }

        public static Image CreateThumbnail(Image original, string widthCondition, string heightCondition)
        {
            int width = 0;
            int thumbnailWidth = 0;
            string widthConditionSymbol = "";
            int height = 0;
            int thumbnailHeight = 0;
            string heightConditionSymbol = "";
            if (widthCondition.StartsWith("="))
            {
                widthConditionSymbol = "=";
                if (widthCondition.EndsWith("%"))
                {
                    thumbnailWidth = (int)((int.Parse(widthCondition.Substring(1).TrimEnd(new char[] { '%' })) * original.Width) * 0.01);
                }
                else
                {
                    thumbnailWidth = int.Parse(widthCondition.Substring(1));
                }
            }
            else
            {
                widthConditionSymbol = "<=";
                if (widthCondition.EndsWith("%"))
                {
                    thumbnailWidth = (int)((int.Parse(widthCondition.Substring(2).TrimEnd(new char[] { '%' })) * original.Width) * 0.01);
                }
                else
                {
                    thumbnailWidth = int.Parse(widthCondition.Substring(2));
                }
            }
            if (heightCondition.StartsWith("="))
            {
                heightConditionSymbol = "=";
                if (heightCondition.EndsWith("%"))
                {
                    thumbnailHeight = (int)((int.Parse(heightCondition.Substring(1).TrimEnd(new char[] { '%' })) * original.Height) * 0.01);
                }
                else
                {
                    thumbnailHeight = int.Parse(heightCondition.Substring(1));
                }
            }
            else
            {
                heightConditionSymbol = "<=";
                if (heightCondition.EndsWith("%"))
                {
                    thumbnailHeight = (int)((int.Parse(heightCondition.Substring(2).TrimEnd(new char[] { '%' })) * original.Height) * 0.01);
                }
                else
                {
                    thumbnailHeight = int.Parse(heightCondition.Substring(2));
                }
            }
            float heigthScale = ((float)thumbnailHeight) / ((float)original.Height);
            float widthScale = ((float)thumbnailWidth) / ((float)original.Width);
            if ((widthConditionSymbol == "=") && (heightConditionSymbol == "="))
            {
                width = thumbnailWidth;
                height = thumbnailHeight;
            }
            else if (widthConditionSymbol == "=")
            {
                int newHeight = (int)(original.Height * widthScale);
                width = thumbnailWidth;
                height = (newHeight <= thumbnailHeight) ? newHeight : thumbnailHeight;
            }
            else if (heightConditionSymbol == "=")
            {
                int newWidth = (int)(original.Width * heigthScale);
                width = (newWidth <= thumbnailWidth) ? newWidth : thumbnailWidth;
                height = thumbnailHeight;
            }
            else
            {
                if ((original.Width <= thumbnailWidth) && (original.Height <= thumbnailHeight))
                {
                    return original;
                }
                float newScale = (widthScale >= heigthScale) ? heigthScale : widthScale;
                width = (int)(original.Width * newScale);
                height = (int)(original.Height * newScale);
            }
            return CreateThumbnail(original, width, height);
        }

        public static Image CreateWatermark(Image originalImg, Image watermarkImg, int x, int y)
        {
            Bitmap bitmap = new Bitmap(originalImg.Width, originalImg.Height, PixelFormat.Format24bppRgb);
            bitmap.MakeTransparent();
            Graphics originalGraphics = Graphics.FromImage(bitmap);
            originalGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            originalGraphics.DrawImage(originalImg, new Rectangle(0, 0, originalImg.Width, originalImg.Height), 0, 0, originalImg.Width, originalImg.Height, GraphicsUnit.Pixel);
            originalGraphics.Dispose();
            bitmap.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
            Graphics watermarkGraphics = Graphics.FromImage(bitmap);
            watermarkGraphics.DrawImage(watermarkImg, new Rectangle(x, y, watermarkImg.Width, watermarkImg.Height), 0, 0, watermarkImg.Width, watermarkImg.Height, GraphicsUnit.Pixel);
            watermarkGraphics.Dispose();
            return bitmap;
        }

        public static Image CreateWatermark(Image originalImg, Image watermarkImg, System.Web.UI.WebControls.HorizontalAlign align, System.Web.UI.WebControls.VerticalAlign valign)
        {
            int x = 0;
            int y = 0;
            int marginX = originalImg.Width - watermarkImg.Width;
            int marginY = originalImg.Height - watermarkImg.Height;
            switch (align)
            {
                case System.Web.UI.WebControls.HorizontalAlign.Left:
                    x = 5;
                    break;

                case System.Web.UI.WebControls.HorizontalAlign.Center:
                    x = ((marginX / 2) <= 5) ? 5 : (marginX / 2);
                    break;

                case System.Web.UI.WebControls.HorizontalAlign.Right:
                    x = (marginX <= 10) ? 5 : (marginX - 5);
                    break;

                default:
                    x = 5;
                    break;
            }
            switch (valign)
            {
                case System.Web.UI.WebControls.VerticalAlign.Top:
                    y = 5;
                    break;

                case System.Web.UI.WebControls.VerticalAlign.Middle:
                    y = ((marginY / 2) <= 5) ? 5 : (marginY / 2);
                    break;

                case System.Web.UI.WebControls.VerticalAlign.Bottom:
                    y = (marginY <= 10) ? 5 : (marginY - 5);
                    break;

                default:
                    y = 5;
                    break;
            }
            return CreateWatermark(originalImg, watermarkImg, x, y);
        }

        public static Image CreateWatermark(Image originalImg, string text, int x, int y)
        {
            Bitmap bitmap = new Bitmap(originalImg.Width, originalImg.Height, PixelFormat.Format24bppRgb);
            bitmap.MakeTransparent();
            Graphics originalGraphics = Graphics.FromImage(bitmap);
            originalGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            originalGraphics.DrawImage(originalImg, new Rectangle(0, 0, originalImg.Width, originalImg.Height), 0, 0, originalImg.Width, originalImg.Height, GraphicsUnit.Pixel);
            originalGraphics.Dispose();
            Font font = new Font("Arial", 10f);
            Brush brush = new SolidBrush(Color.Black);
            bitmap.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
            Graphics watermarkGraphics = Graphics.FromImage(bitmap);
            watermarkGraphics.DrawString(text, font, brush, (PointF)new Point(x, y));
            font.Dispose();
            brush.Dispose();
            watermarkGraphics.Dispose();
            return bitmap;
        }

        public static Image CreateWatermark(Image originalImg, string text, System.Web.UI.WebControls.HorizontalAlign align, System.Web.UI.WebControls.VerticalAlign valign)
        {
            int x = 0;
            int y = 0;
            Font font = new Font("Arial", 10f);
            int marginX = originalImg.Width - ((int)(font.SizeInPoints * text.Length));
            int marginY = originalImg.Height - ((int)(font.SizeInPoints * 2f));
            switch (align)
            {
                case System.Web.UI.WebControls.HorizontalAlign.Left:
                    x = 5;
                    break;

                case System.Web.UI.WebControls.HorizontalAlign.Center:
                    x = ((marginX / 2) <= 5) ? 5 : (marginX / 2);
                    break;

                case System.Web.UI.WebControls.HorizontalAlign.Right:
                    x = (marginX <= 10) ? 5 : (marginX - 5);
                    break;

                default:
                    x = 5;
                    break;
            }
            switch (valign)
            {
                case System.Web.UI.WebControls.VerticalAlign.Top:
                    y = 5;
                    break;

                case System.Web.UI.WebControls.VerticalAlign.Middle:
                    y = ((marginY / 2) <= 5) ? 5 : (marginY / 2);
                    break;

                case System.Web.UI.WebControls.VerticalAlign.Bottom:
                    y = (marginY <= 10) ? 5 : (marginY - 5);
                    break;

                default:
                    y = 5;
                    break;
            }
            font.Dispose();
            return CreateWatermark(originalImg, text, x, y);
        }

        public static Stream ImageToStream(Image image)
        {
            Stream stream = new MemoryStream();
            new Bitmap(image).Save(stream, ImageFormat.Jpeg);
            stream.Position = 0L;
            return stream;
        }

        public static Image StreamToImage(Stream stream)
        {
            Image image = Image.FromStream(stream);
            stream.Position = 0L;
            return image;
        }
    }


}
